package org.apache.servicecomb.fence.resource;

import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@RestSchema(schemaId = "AccessingDataJpaEndpoint")
@RequestMapping(path = "/v1/jpa")
public class AccessingDataJpaEndpoint {
    private CustomerRepository repository;

    @Autowired
    public AccessingDataJpaEndpoint(CustomerRepository repository) {
        this.repository = repository;
    }

    @PutMapping("/save")
    public Customer save(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName) {
        return repository.save(new Customer(firstName, lastName));
    }

    @GetMapping("findById")
    public Customer findById(@RequestParam("id") long id) {
        return repository.findById(id);
    }

    @GetMapping("findByLastName")
    public List<Customer> findByLastName(@RequestParam("lastName") String lastName) {
        return repository.findByLastName(lastName);
    }
}
